<?php
namespace model;

/**
 * zapisuje modele do asocjayjnej tablicy
 *
 * @author Damian
 */
class DataTable {
    private $modelData=array();
    private $modelClass;
    private $primaryColumn;
    /**
     * 
     * @param \model\ModelBase[] $models <p>lista modeli zwrócona za pomocą \model\ModelBase::all()<br> lub konkretny model</p>
     * @param boolean $preserveKeys [optional] <p>określa czy kolejne rekordy mają być numerowane przez id rekordu(true) czy kolejno(false)
     * @param  mixded $key [optional] <p>określa która kolumna ma być kluczem</p> 
     * <br/>domyślnie true</p>
     */
    public function __construct($models,$preserveKeys=true,$keyColumn=false) {
        if(is_array($models) and sizeof($models)>0){
            $columns = $models[0]->getColumns();
            $this->modelClass=  get_class($models[0]);
            $newKey=false;
            foreach ($models as $model){
               // var_dump($model);
                $modelColumns=array();
                foreach ($columns as $key=> $column){
                   
                    if ($column->primary)  {
                        $this->primaryColumn=$key;
                    }
                    if (is_object($model->$key)) {
                        $newTable=new DataTable($model->$key,$preserveKeys);
                        $modelColumns[$key] = $newTable->getModelData();
                    }
                    else {
                         if (strcmp($keyColumn, $column->name)==0) {
                        $newKey=$keyColumn;
                        }
                        $modelColumns[$key]=$model->$key;                     
                    }
                }
                $key=$this->primaryColumn;
               if ($preserveKeys){
                   if ($newKey){
                       $this->modelData[$model->$newKey]=$modelColumns;
                   }
                   else{
                       $this->modelData[$model->$key]=$modelColumns;
                   }                   
               }
               else{
                   $this->modelData[]=$modelColumns;
               }
            }
        }
        else if (is_object($models) && !$models instanceof \DateTime){
            $columns = $models->getColumns();
            $modelColumns=array();
            $ColumnKey="";
            foreach ($columns as $key=> $column){
                if ($column->primary)  {
                        $this->primaryColumn=$key;
                        $ColumnKey=$key;
                    }
                if (is_object($models->$key)) {
                    $newTable=new DataTable($models->$key,$preserveKeys);
                    $modelColumns[$key] = $newTable->getModelData();
                }
                else {
                    $modelColumns[$key]=$models->$key;                 
                }
            }
           if ($preserveKeys){
                   $this->modelData=$modelColumns;
               }
               else{
                   $this->modelData=$modelColumns;
               }
        }
        
    }
    /**
     * 
     * @param type $object [optional] <p>false - zwracana jest wielowymiarowa tablica<br>true zwracana jest tablica obiektow STDClass
     * <br>domyślnie false</p>
     * @return array
     */
    public function getModelData($object=false)  {
        if (!$object) {
            return $this->modelData;
        }
        else{
            $result = array();
            foreach ($this->modelData as $key=>$value){
                if (sizeof($this->modelData)==1){}
                $result[$key]=  \library\ArrayMethods::toObject($value);
            }
            return $result;
        }
    }
    public function getObject($index){
       // $data = \library\ArrayMethods::getElement($index, $this->modelData);
        $elem = new $this->modelClass();
        return $elem->getByPrimaryKey($index);
    }
}
